GRANT/REVOKE PRIVILEGE



MySQL - GRANT/REVOKE PRIVILEGE


Grant and Revoke commands are the DCL commands. The GRANT command is used for conferring the authorization to the users whereas REVOKE command is used for withdrawing the authorization. Select, insert, update and delete are some of the privileges that are included in SQL standards.


SYNTAX :

GRANT privileges_names ON object TO user;

Parameters Used:


  • privileges_name: These are the access rights or privileges granted to the user.

  • object:It is the name of the database object to which permissions are being granted. In the case of granting privileges on a table, this would be the table name.

  • user:It is the name of the user to whom the privileges would be granted.


    Privileges :

    PrivilegeDescription
    SELECTAbility to perform SELECT statements on the table.
    INSERTAbility to perform INSERT statements on the table.
    UPDATEAbility to perform UPDATE statements on the table.
    DELETEAbility to perform DELETE statements on the table.
    INDEXAbility to create an index on an existing table.
    CREATEAbility to perform CREATE TABLE statements.
    ALTERAbility to perform ALTER TABLE statements to change the table definition.
    DROPAbility to perform DROP TABLE statements.
    GRANT OPTIONAllows you to grant the privileges that you possess to other users.
    ALLGrants all permissions except GRANT OPTION.


  • object : The name of the database object that you are granting permissions for. In the case of granting privileges on a table, this would be the table name.

  • user : The name of the user that will be granted these privileges.


    Example :


    Let's look at some examples of how to grant privileges on tables in MySQL.


    For example, if you wanted to grant SELECT, INSERT, UPDATE, and DELETE privileges on a table called temporary to a user name root, you would run the following GRANT statement:

    GRANT SELECT, INSERT, DELETE, UPDATE ON temporary  TO 'root'@'localhost' ;
    
    

    You can also use the ALL keyword to indicate that you wish to grant all permissions except GRANT OPTION to a user named root. For example:

    GRANT ALL ON temporary  TO 'root'@'localhost' ;
    
    

    If you wanted to grant only SELECT access on the contacts table to all users, you could grant the privileges to *. For example:

    GRANT SELECT  ON temporary   TO '*'@'localhost' ;
    
    

    Grant Privileges on Functions/Procedures :


    When dealing with functions and procedures, you can grant users the ability to EXECUTE these functions and procedures in MySQL.


    SYNTAX :

    The syntax for granting EXECUTE privileges on a function/procedure in MySQL is:

    GRANT EXECUTE ON [ PROCEDURE | FUNCTION ] object TO user;
    
    

  • EXECUTE : The ability to execute the function or procedure.

  • PROCEDURE : It is used when the privilege is being granted on a procedure in MySQL.

  • FUNCTION : It is used when the privilege is being granted on a function in MySQL.

  • object : The name of the database object that you are granting privileges for. In the case of granting EXECUTE privileges on a function or procedure, this would be the function name or the procedure name.

  • user : The name of the user that will be granted the EXECUTE privileges.


    Example - Function :


    Let's look at some examples of how to grant EXECUTE privileges on a function in MySQL.

    For example, if you had a function called Newfunction and you wanted to grant EXECUTE access to the user named root, you would run the following GRANT statement:

    GRANT EXECUTE ON FUNCTION Newfunction TO 'root'@'localhost' ;
    
    

    If you wanted to grant ALL users the ability to EXECUTE this function, you would run the following GRANT statement:

    GRANT EXECUTE ON FUNCTION Newfunction TO '*'@'localhost';
    
    

    Example - Procedure :


    Let's look at some examples of how to grant EXECUTE privileges on a procedure in MySQL.

    For example, if you had a procedure called Sprocedure1 and you wanted to grant EXECUTE access to the user named root, you would run the following GRANT statement:

    GRANT EXECUTE ON PROCEDURE Sprocedure1 TO 'root'@'localhost' ;
    
    
    

    If you wanted to grant ALL users the ability to EXECUTE this procedure, you would run the following GRANT statement:

    GRANT EXECUTE ON PROCEDURE Sprocedure1 TO '*'@'localhost';
    

    Checking the Privileges Granted to a User:


    To see the privileges granted to a user in a table, the SHOW GRANTS statement is used. To check the privileges granted to a user named "root" and host as "localhost", the following SHOW GRANTS statement will be executed:


    SHOW GRANTS FOR 'root'@'localhost' ;
    
    
    Grants for root@localhost
    GRANT ALL PRIVILEGES ON *.* 'root'@'localhost' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' WITH GRANT OPTION
    GRANT ALL PRIVILEGES ON 'firstdatabase'.'temporary' TO 'root'@'localhost'
    GRANT EXECUTE ON PROCEDURE 'firstdatabase'.'sprocedure1' TO 'root'@'localhost'
    GRANT EXECUTE ON FUNCTION 'firstdatabase'.'newfunction' TO 'root'@'localhost'
    
    
    
    
    

    Revoking Privileges from a Table :


    Once you have granted privileges, you may need to revoke some or all of these privileges. To do this, you can run a revoke command. You can revoke any combination of SELECT, INSERT, UPDATE, DELETE, REFERENCES, ALTER, or ALL.


    SYNTAX :

    The syntax for revoking privileges on a table in MySQL is:


    REVOKE privileges ON object FROM user;
    

    privileges

    It can be any of the following values:

    PrivilegeDescription
    SELECTAbility to perform SELECT statements on the table.
    INSERTAbility to perform INSERT statements on the table.
    UPDATEAbility to perform UPDATE statements on the table.
    DELETEAbility to perform DELETE statements on the table.
    INDEXAbility to create an index on an existing table.
    CREATEAbility to perform CREATE TABLE statements.
    ALTERAbility to perform ALTER TABLE statements to change the table definition.
    DROPAbility to perform DROP TABLE statements.
    GRANT OPTIONAllows you to grant the privileges that you possess to other users.
    ALLGrants all permissions except GRANT OPTION.


  • object : The name of the database object that you are revoking privileges for. In the case of revoking privileges on a table, this would be the table name.

  • user : The name of the user that will have these privileges revoked.


    Example :


    Let's look at some examples of how to revoke privileges on tables in MySQL.

    For example, if you wanted to revoke DELETE and UPDATE privileges on a table called temporary from a user named root, you would run the following REVOKE statement:

    REVOKE DELETE, UPDATE ON temporary FROM 'root'@'localhost' ;
    

    If you wanted to revoke all permissions (except GRANT OPTION) on a table for a user named root, you could use the ALL keyword as follows:

    REVOKE ALL ON temporary FROM 'root'@'localhost' ;
    
    

    If you had granted SELECT privileges to * (ie: all users) on the temporary table and you wanted to revoke these privileges, you could run the following REVOKE statement:

    REVOKE SELECT  ON temporary FROM 'root'@'localhost' ;
    
    

    Revoke Privileges on Functions/Procedures :


    Once you have granted EXECUTE privileges on a function or procedure, you may need to REVOKE these privileges from a user in MySQL. To do this, you can execute a REVOKE command.


    SYNTAX :
    REVOKE EXECUTE ON [ PROCEDURE | FUNCTION ] object FROM user;
    
    


  • EXECUTE : The ability to execute the function or procedure is being revoked.

  • PROCEDURE : It is used when the privilege is being revoked on a procedure in MySQL.

  • FUNCTION : It is used when the privilege is being revoked on a function in MySQL.

  • object : The name of the database object that you are revoking privileges for. In the case of revoking EXECUTE privileges on a function or procedure, this would be the function name or the procedure name.

  • user:The name of the user that will be revoked the EXECUTE privileges.


    Example - Function

    Let's look at some examples of how to revoke EXECUTE privileges on a function in MySQL.

    If you wanted to revoke EXECUTE privileges on a function called Newfunction from a user named root, you would run the following REVOKE statement:

    REVOKE EXECUTE ON FUNCTION Newfunction FROM 'root'@'localhost' ;
    
    

    If you had granted EXECUTE privileges to * (all users) on the function called Newfunction and you wanted to revoke these EXECUTE privileges, you could run the following REVOKE statement:

    GRANT EXECUTE ON FUNCTION Newfunction TO '*'@'localhost';
    
    

    Example - Procedure

    Let's look at some examples of how to revoke EXECUTE privileges on a procedure in MySQL.

    If you wanted to revoke EXECUTE privileges on a procedure called Sprocedure1 from a user named root, you would run the following REVOKE statement:

    REVOKE EXECUTE ON PROCEDURE  Sprocedure1 FROM 'root'@'localhost' ;
    
    

    If you had granted EXECUTE privileges to * (all users) on the procedure called Sprocedure1 and you wanted to revoke these EXECUTE privileges, you could run the following REVOKE statement:

    GRANT EXECUTE ON PROCEDURE Sprocedure1 TO '*'@'localhost' ;
    
    

    GRANT EXECUTE ON PROCEDURE Sprocedure1 TO '*'@'localhost' ;
    
    


    MySQL TRIGGERS

    MySQL - Triggers

    posted on 2019-11-29 21:44:07 - mysql Tutorials


    Grant_ Revoke Privilege

    MySQL - Grant_ Revoke Privilege

    posted on 2019-11-26 23:15:04 - mysql Tutorials


    MySQL Vs SQL

    MySQL Vs SQL

    posted on 2019-11-25 05:02:26 - mysql Tutorials


    Prompt Examples

    ChatGPT Prompt Examples

    posted on 2023-06-21 22:37:19 - ChatGPT Tutorials


    Use Cases

    Chat GPT Key Use Cases

    posted on 2023-06-21 21:03:17 - ChatGPT Tutorials


    Prompt Frameworks

    Prompt Frameworks

    posted on 2023-06-21 19:33:06 - ChatGPT Tutorials